package com.ttg.controller;
import com.ttg.utils.TLinx2Util;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.Map;
import java.util.TreeMap;
@Controller
@RequestMapping("/callback")
public class TLinx2CallbackController {
  /**
   * Field LOGGER
   * Description
   */
  private static final Logger LOGGER = LoggerFactory.getLogger(TLinx2CallbackController.class);
  @RequestMapping(value = "/scanpay_cashier/payResult", method = RequestMethod.GET)
  @ResponseBody
  public String testPayCallback(HttpServletRequest request) {
    LOGGER.debug("接收到的支付回调信息为：{}", request);
    Map<String, String> params = new TreeMap<>();
    // 取出所有参数是为了验证签名
    Enumeration<String> parameterNames = request.getParameterNames();
    while (parameterNames.hasMoreElements()) {
      String parameterName = parameterNames.nextElement();
      params.put(parameterName, request.getParameter(parameterName));
    }
    JSONObject paramsObject = JSONObject.fromObject(params);
    TLinx2Util.verifySign(paramsObject);
    System.out.println("===========回调参数是：" + paramsObject.toString());
    return "notify_success";
  }
}


